<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "sys_user2role".
 *
 * @property int $id ID
 * @property int $userid 用户ID
 * @property int $roleid 角色ID
 * @property string $addtime
 */
class SysUser2role extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_user2role';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid', 'roleid', 'addtime'], 'required'],
            [['userid', 'roleid', 'addtime'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户ID',
            'roleid' => '角色ID',
            'addtime' => 'Addtime',
        ];
    }
    
    public static function getUserSource($userid){
        $sql = "select id,name,url,parent,ismenu from `sys_source` where id in (select sourceid from `sys_role2source` where roleid in (select roleid from `sys_user2role` where userid=$userid)) order by id asc";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    public static function getUserRoleId($userid){
        $sql = "select roleid from sys_user2role where userid=$userid";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryOne();
        return $model['roleid'];
    }
}
